#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
main(){
  freopen("boast.in","r",stdin);
  freopen("boast.out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n,c=1e16; cin>>n;
  vector<pii> a(n);
  for(auto &i:a)cin>>i.first>>i.second;
  sort(a.begin(),a.end(),[&](pii x,pii y){
    return x.second<y.second;
  });
  multiset<int> t1,t2;
  for(int i=0;i<n;i++)
    t2.emplace(a[i].first+a[i].second);
  for(int i=0;i<n;i++){
    t2.erase(t2.find(a[i].first+a[i].second));
    vector<int> v;
    if(!t1.empty()){
      v.emplace_back(*t1.begin()+a[i].second);
      if(t1.size()>1)
        v.emplace_back(*next(t1.begin())+a[i].second);
    }
    if(!t2.empty()){
      v.emplace_back(*t2.begin());
      if(t2.size()>1)
        v.emplace_back(*next(t2.begin()));
    }
    sort(v.begin(),v.end());
    c=min(c,v[0]+a[i].first+v[1]);
    t1.emplace(a[i].first);
  }
  cout<<c<<endl;
  return 0;
}